<template>
  <q-tab-panel class="flex items-center justify-center">
    <q-card>
      <q-card-section class="row items-center q-gutter-x-md">
        <div class="text-h6">订单服务自动化配置</div>
        <q-space/>
        <q-toggle v-model="config.orderAuto" label="自动分单" @click="allocateOpen"/>
      </q-card-section>
      <q-separator/>
      <q-card-section>
        <q-list bordered separator>
          <q-item>
            <q-item-section>订单分配模式</q-item-section>
            <q-item-section>
              <q-select readonly outlined v-model="config.orderAutoMode" :options="orderAutoMode" map-options emit-value/>
            </q-item-section>
          </q-item>
          <q-item>
            <q-item-section>机器人排序方式</q-item-section>
            <q-item-section>
              <q-select readonly outlined v-model="config.amrSortRule" :options="amrSortRule" map-options emit-value/>
            </q-item-section>
          </q-item>
          <q-item>
            <q-item-section>分配轮数</q-item-section>
            <q-item-section side>
              {{config.recheckCount}}轮
            </q-item-section>
          </q-item>
          <q-item>
            <q-item-section>是否忽略正在执行任务的机器人</q-item-section>
            <q-item-section side>
              <q-toggle disable v-model="config.ignoreNullAmr"/>
            </q-item-section>
          </q-item>
          <q-item>
            <q-item-section>是否忽略起点有机器人的订单</q-item-section>
            <q-item-section side>
              <q-toggle disable v-model="config.ignoreStartPose"/>
            </q-item-section>
          </q-item>
          <q-item>
            <q-item-section>是否启用点位筛选功能</q-item-section>
            <q-item-section side>
              <q-toggle disable v-model="config.filterByPoseName"/>
            </q-item-section>
          </q-item>
        </q-list>
      </q-card-section>
    </q-card>
  </q-tab-panel>
</template>

<script>
import { onMounted, ref } from 'vue'
import { api } from 'boot/axios.js'
import { useAPIStore } from 'stores/api/index.js'
import { useQuasar } from 'quasar'

const orderAutoMode = [
  { label: '机器人类型', value: 'type' },
  { label: '机器人型号', value: 'model' }
]

const amrSortRule = [
  { label: '无排序', value: 'empty' },
  { label: '依据电量倒序', value: 'batteryNumber' },
  { label: '依据电压倒序', value: 'batteryVolt' }
]

export default {
  name: 'OrderConfig',
  setup () {
    const $q = useQuasar()
    const config = ref({})
    function getConfig () {
      api.get(useAPIStore().orderUrl + 'automate/config').then(rs => {
        config.value = rs.data
      })
    }

    onMounted(getConfig)

    return {
      config,
      orderAutoMode,
      amrSortRule,
      allocateOpen () {
        api.get(useAPIStore().orderUrl + 'automate/set/open/' + config.value.orderAuto).then(rs => {
          if (rs.data === 'done') {
            if (config.value.orderAuto) {
              $q.notify({ message: '自动分单已打开', type: 'positive', position: 'top' })
            } else {
              $q.notify({ message: '自动分单已关闭', type: 'positive', position: 'top' })
            }
            getConfig()
          }
        })
      }
    }
  }
}
</script>
